package com.zyx.thread1;

public class Bank {
    private int account=100;//账户余额
    //取钱
    public synchronized int getMoney(int order){
        //当账户余额大于取款金额，则正常取款
        if(account>=order){
            System.out.println("系统正在处理。。。。。。。");
            try {
                //模拟等待时间
                Thread.sleep((long) ((Math.random()*2000)+600));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //取款结束后，账户余额发生改变。当前余额为账户余额减去取款金额
            account-=order;
            System.out.println("尊敬的"+Thread.currentThread().getName()+"用户，您本次的取款金额是"
                    +order+"元，当前账户余额为"+account+"元");
            //取款正常，返回取款金额
            return order;
        }else {
            //若取款金额大于账户余额，则取款失败，提示账户余额不足
            System.out.println("很抱歉，余额不足。。。");
            //不能正常取款，取款金额为0
            return 0;
        }
    }
    //咨询业务
    public synchronized void ask(){
        System.out.println(Thread.currentThread().getName()+"正在咨询业务");
        try {
            Thread.sleep((long) ((Math.random()*2000)+600));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"咨询业务完毕");


    }
}
